<?php

namespace App\Http\Controllers;

use App\Models\Activity;
use App\Models\ActivityQuestion;
use App\Models\ActivitySign;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use \Mail;
use \Excel;

class ActivitySignController extends CommonController
{
    /**
     * 获取活动签到列表
     */
    public function getIndex(Request $request)
    {
        $data = $request->input();
        $lists = ActivitySign::with('user')->where('activity_id', $data['id'])->paginate(10);

        return view('activitysign.index')->with('lists', $lists)->with('id', $data['id'])->with('actions', $this->actions);
    }

    public function postSend(Request $request)
    {
        $data = $request->input();
        if (!isset($data['email']) || $data['email'] == '') {
            return $this->tip('请输入邮箱地址', url('activitysign/index?id=' . $data['id']));
        }
        $emails = explode(',', str_replace('，', ',', trim($data['email'])));

        //剔除可能出现的连续两个逗号
        $emails = array_filter($emails,function ($v) {if ($v == '') {return false;} else {return true;}});
        $emails = array_map(function ($v) {return trim($v);},$emails);
        foreach ($emails as $k => $v) {
            if (!checkEmail($v)) {
                return $this->tip('你的输入，至少存在一个不符合规范的邮箱地址', url('activitysign/index?id=' . $data['id']));
            }
        }

        $lists = ActivitySign::where('activity_id', $data['id'])->get();
        $title = [
            [
                "nickname" => '昵称',
                "realname" => '真实姓名',
                "username" => 'username',
                "created_at" => '签到时间',
                "address" => '签到地点',

            ],
        ];

        $signInfo = array();
        foreach ($lists as $k => $v) {
            $v->user ? $signInfo[$k]['nickname'] = $v->user->nickname:$signInfo[$k]['nickname'] = '';
            $v->user ? $signInfo[$k]['realname'] = $v->user->nickname:$signInfo[$k]['realname'] = '';
            $v->user ? $signInfo[$k]['username'] = $v->user->nickname:$signInfo[$k]['username'] = '';
            $signInfo[$k]['created_at'] = $v->created_at->toDateTimeString();
            $signInfo[$k]['address'] = $v->address;
        }
        $signInfo = array_merge($title, $signInfo);

        $excel_path = config('upload.uploadDir');
//        dd($excel_path.'\exports');
        Excel::create(
            'sign',
            function ($excel) use ($signInfo) {
                $excel->sheet(
                    '签到表',
                    function ($sheet) use ($signInfo) {
                        $sheet->rows($signInfo);
                    }
                );
            }
        )->store('xls', $excel_path);

        $datas = ['email' => $emails, 'lists' => $lists];
        Mail::send(
            'activitysign.email',
            $datas,
            function ($message) use ($datas, $excel_path) {
                foreach ($datas['email'] as $k => $v) {
                    $message->to($v)->subject('签到信息')->attach($excel_path . '/sign.xls');
                }
            }
        );

        return $this->tip('恭喜,邮件发送成功', url('activitysign/index?id=' . $data['id']),true);
    }
}
